//
//  ApplyTableViewCell.m
//  DanaKreditLoan
//
//  Created by ZhengChengFeng on 2024/10/22.
//

#import "ApplyTableViewCell.h"

@implementation ApplyTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        self.backV = [[UIView alloc] init];
        self.backV.backgroundColor = kWhiteColor;
        [self.contentView addSubview:self.backV];
        [self.backV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(15);
            make.right.equalTo(self.contentView.mas_right).offset(-15);
            make.top.equalTo(self.contentView.mas_top);
            make.bottom.equalTo(self.contentView.mas_bottom);
        }];
        
        self.whiteV = [[UIView alloc] init];
        self.whiteV.backgroundColor = kWhiteColor;
        [self.backV addSubview:self.whiteV];
        [self.whiteV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.backV.mas_left).offset(15);
            make.right.equalTo(self.backV.mas_right).offset(-15);
            make.top.equalTo(self.backV.mas_top);
            make.bottom.equalTo(self.backV.mas_bottom);
        }];
        self.whiteV.layer.cornerRadius = 10;
        self.whiteV.layer.masksToBounds = YES;
        self.whiteV.layer.borderWidth = 1;
        self.whiteV.layer.borderColor = [UIColor colorWithHexString:@"#EFEFEF"].CGColor;
        
        
        self.iconV = [[UIImageView alloc] init];
        [self.whiteV addSubview:self.iconV];
        [self.iconV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.whiteV.mas_right);
            make.width.equalTo(@46);
            make.height.equalTo(@58);
            make.top.equalTo(self.whiteV.mas_top);
        }];
        
        self.titleLab = [UILabel createLabel:[UIFont fontWithName:kFutura size:15] color:[UIColor colorWithHexString:@"#6C6C6C"]];
        [self.whiteV addSubview:self.titleLab];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.whiteV.mas_left).offset(15);
            make.right.equalTo(self.whiteV.mas_right).offset(-5);
            make.top.equalTo(self.whiteV.mas_top).offset(15);
            make.height.equalTo(@20);
        }];
        
        self.textFiled = [[UITextFieldHelper alloc] init];
        self.textFiled.textColor = kBlackColor;
        self.textFiled.font = [UIFont fontWithName:kFutura size:15];
        self.textFiled.delegate = self;
        [self.whiteV addSubview:self.textFiled];
        [self.textFiled mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.titleLab.mas_left);
            make.right.equalTo(self.whiteV.mas_right).offset(-5 - 11);
            make.bottom.equalTo(self.whiteV.mas_bottom);
            make.top.equalTo(self.titleLab.mas_bottom);
        }];
       
        [self.textFiled addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        
        
        self.arrowImgView = [[UIImageView alloc] init];
        [self.whiteV addSubview:self.arrowImgView];
        [self.arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@11);
            make.height.equalTo(@8);
            make.right.equalTo(self.whiteV.mas_right).offset(-5);
            make.centerY.equalTo(self.textFiled.mas_centerY);
        }];
        
        
    }
    return self;
}

- (void)setModel:(ApplyModel *)model{
    _model = model;
    self.titleLab.text = esString(model.arrow);
    
    self.iconV.image = [UIImage imageNamed:@"icon_rl"];
    
    self.arrowImgView.image = [UIImage imageNamed:@"icon_idcard8"];
    if ([model.bridge isEqualToString:kBeen] || [model.bridge isEqualToString:kProvender]) {
        self.textFiled.userInteractionEnabled = NO;
        self.arrowImgView.hidden = NO;
    } else {
        self.textFiled.userInteractionEnabled = YES;
        self.arrowImgView.hidden = YES;
    }
    
    self.textFiled.text = esString(model.favour);
    self.textFiled.placeholder = esString(model.dressings);
    
    if (!PMStringIsEmpty(model.dressings)) {
        NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:self.textFiled.placeholder attributes:@{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#EFEFEF"]}];
        self.textFiled.attributedPlaceholder = placeholderString;
    }
    
    if ([model.wandered integerValue] == 1) {
        self.textFiled.keyboardType = UIKeyboardTypeNumberPad;
    } else {
        self.textFiled.keyboardType = UIKeyboardTypeDefault;
    }
    
}

- (void)textFieldDidChange:(UITextField *) textField{
    NSString *textString = esString(textField.text);
    self.model.favour = textString;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
